package org.example.blogserver.service.impl;

import org.example.blogserver.dao.SysUserDao;
import org.example.blogserver.entity.SysUser;
import org.example.blogserver.handler.TokenManager;
import org.example.blogserver.service.SysUserService;
import org.example.blogserver.vo.Result;
import org.example.blogserver.vo.UserVo;
import org.example.blogserver.vo.params.PasswordParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserDao userDao;

    @Autowired
    private TokenManager tokenManager;

    @Override
    public Result insertUser(SysUser user) {
        int result=userDao.insertUser(user);
        if (result > 0) {
            return Result.success("添加成功！",null);
        } else {
            return Result.fail(500, "添加失败！");
        }
    }

    @Override
    public Result updatePassword(String token, PasswordParam passwordParam) {
        // 验证 token 是否有效
        String account = tokenManager.getAccountFromToken(token);
        if (account == null) {
            return Result.fail(400, "登录过期，请重新登录！");
        }

        // 获取用户信息
        SysUser user = userDao.findUserByAccount(account);
        if (user == null) {
            return Result.fail(400, "用户不存在！");
        }

        // 验证旧密码是否正确
        if (!user.getPassword().equals(passwordParam.getOldPassword())) {
            return Result.fail(400, "旧密码错误！");
        }

        // 执行修改密码操作
        userDao.updatePasswordByAccount(account, passwordParam.getNewPassword());
        return Result.success("密码修改成功！", null);
    }

    @Override
    public Result getUserInfoByAccount(String account) {
        SysUser user = userDao.findUserByAccount(account);
        if (user == null) {
            return Result.fail(400, "用户不存在！");
        }
        UserVo userVo=convertToUserVo(user);
        return Result.success("获取用户信息成功！", userVo);
    }
    @Override
    public UserVo getUserVoByAccount(String account) {
        SysUser user = userDao.findUserByAccount(account);
        if (user == null) {
            return null;
        }
        return convertToUserVo(user);
    }
    @Override
    public Result getUserInfo(String account, String password) {
        SysUser user = userDao.findUserByAccount(account);
        if (user == null) {
            return Result.fail(400, "用户不存在！");
        } else if (!Objects.equals(user.getPassword(), password)) {
            return Result.fail(400, "密码错误！");
        }else {
            UserVo userVo=convertToUserVo(user);
            return Result.success("获取用户信息成功！", userVo);
        }
    }

    @Override
    public Result getUserList() {
        List<SysUser> userList = userDao.getUserList();
        return Result.success("获取用户列表成功！", userList);
    }

    @Override
    public Result deleteUser(String token, String account) {
        // 验证 token 是否有效
        String ad_account = tokenManager.getAccountFromToken(token);
        if (ad_account == null) {
            return Result.fail(400, "登录过期，请重新登录！");
        }

        // 执行删除用户操作
        int result=userDao.deleteUserByAccount(account);
        if (result > 0) {
            return Result.success("用户删除成功！", null);
        } else {
            return Result.fail(500, "删除失败！");
        }
    }
    @Override
    public Result updateNicknameByAccount(String token, String newNickname) {
        String account = tokenManager.getAccountFromToken(token);
        int result=userDao.updateNicknameByAccount(account,newNickname);
        if (result > 0) {
            return Result.success("修改成功！",null);
        } else {
            return Result.fail(500, "修改失败！");
        }
    }

    @Override
    public Result updateAvatarByAccount(String account, byte[] newAvatar) {
        int result=userDao.updateAvatarByAccount(account,newAvatar);
        if (result > 0) {
            return Result.success("上传成功！",null);
        } else {
            return Result.fail(500, "上传失败！");
        }
    }
    @Override
    public UserVo convertToUserVo(SysUser sysUser) {
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser, userVo);
        return userVo;
    }
}
